<template>
    <div v-loading="loading">
        <basic-infor-mation  @minProfitRateChange="minProfitRateChange" :mchId="mchId" :channelId="channelId" :merchantInfo="merchantInfo" :userinfo="userinfo" @userTypeChange="userTypeChange" ref="basicInfo"/>
        <detail-info v-if="mchId" :merchantInfo="merchantInfo" :userType="userType" :regionList="regionList"/>
        <personal-inlet  :channelId="channelId" ref="personal" v-show="userType == '1' && !mchId" :minProfitRate="minProfitRate" :dLimitList="dLimitList" :mccList1="mccList1" :regionList="regionList" :secondaryList="secondaryList"/>
        <enterprise-inlet  ref="enterprise" :channelId="channelId" v-show="userType == '2' && !mchId" :minProfitRate="minProfitRate" :dLimitList="dLimitList" :regionList="regionList" :mccList1="mccList1" :secondaryList="secondaryList"/>
        <!-- <settlement-config /> -->
        <cashOut-config ref="cashOut" v-show="!mchId" :qry_cash_config_list="qry_cash_config_list" @cashOutConfigChange="cashOutConfigChange"/>
        <el-card class="submit">
          <el-button icon="el-icon-check" :loading="btnLoading" type="primary" @click="submit">提交</el-button>
        </el-card>
    </div>
</template>
<script>
import basicInformation from "./components/basicInformation.vue"
import personalInlet from './components/personalInlet.vue'
import enterpriseInlet from './components/enterpriseInlet.vue'
import settlementConfig from "./components/settlementConfig.vue"
import detailInfo from "./components/detailInfo.vue"
import cashOutConfig from "./components/cashOutConfig.vue"
import { getAreaAll } from '@/api/fetchApi.js'
import { dLimitList } from '../../../../utils/bank.js'
import { bindSaveMerchantByChannel } from '@/api/fetchApi.js'
import {   querySaveMerchant , singleChannelModify , merchantGet , getTopMccByType , singleChannelSave , arealist  , parentlist } from '@/api/api'
export default{
    components:{
        'detail-info':detailInfo,
        'personal-inlet':personalInlet,
        'basic-infor-mation':basicInformation,
        'enterprise-inlet':enterpriseInlet,
        'settlement-config':settlementConfig,
        'cashOut-config':cashOutConfig,
    },
   data(){
     return{
        loading:false,
        userType:'2',
        regionList:[], //省市区
        secondaryList:[], //省市
        mccList1:[],
        dLimitList:dLimitList,
        userinfo:{},
        merchantId:'',
        cashConfig:[],
        qry_cash_config_list:[],
        merchantInfo:{},
        btnLoading:false,
        channelId:'',
        minProfitRate:0,
        mchId:''
     }
   },
   mounted(){
     this.initGetTopMccByType()
     this.initGetAreaAll()
     this.initGetBankInfolist()
     this.userinfo = JSON.parse(sessionStorage.getItem("userInfo"))
     this.merchantId = this.$route.query.mid
     this.mchId = this.$route.query.mchId
     this.channelId = this.$route.query.channelId
     if(this.userinfo.rolesId == 4){
        this.$refs.basicInfo.initByChannel(this.userinfo.fid,this.channelId)
     }
     if(this.mchId){
        this.initMerchantInfo('bind')
        this.$refs.basicInfo.initGetservice()
     }else{
       if(this.merchantId){
         this.initMerchantInfo('Inlet')
       }else{
         this.$refs.basicInfo.initGetservice()
       }
     }
     
   },
   methods:{
    async initMchIdInfo(){
       this.merchantInfo = res.data
    },
    async initMerchantInfo(type){
        let res = {}
         if(type == 'bind'){
            this.loading = true
            res = await   querySaveMerchant({mchId:this.mchId , channelId:this.channelId})
         }else{
            res = await merchantGet(this.merchantId)
         }

         if(res.code == 200){
          this.loading = false
          res.data.mccIdArr = [res.data.merchantMcc1 ,res.data.merchantMcc2 ,res.data.merchantMcc3]
          res.data.detailRegion = [ res.data.provinceCode * 1 , res.data.cityCode * 1,res.data.areaId]
          this.qry_cash_config_list =  res.data.cashConfig  ? JSON.parse(res.data.cashConfig) : []
          if(res.data.fileInfoList){
            JSON.parse(res.data.fileInfoList).forEach(item=>{
            if(item.file_type == 'F13'){
              res.data.pBankcardAFileId = item.file_id
            }else if(item.file_type == 'F02' || item.file_type == 'F40'){
              res.data.pIdcardBFileId = item.file_id
            }else if(item.file_type == 'F07'){
              res.data.pLicenseFileId = item.file_id
            }else if(item.file_type == 'F22'){
              res.data.pOutsideFileId = item.file_id
            }else if(item.file_type == 'F24'){
              res.data.pInsideFileId = item.file_id
            }else if(item.file_type == 'F105'){
              res.data.pCashierFileId = item.file_id
            }else if(item.file_type == 'F03' || item.file_type == 'F41'){
              res.data.pIdcardAFileId = item.file_id
            }else if(item.file_type == 'F08'){
              res.data.pOpenAccountFileId = item.file_id
            }else if(item.file_type == 'F56'){
              res.data.pBankIdcardAFileId = item.file_id
            }else if(item.file_type == 'F55'){
              res.data.pBankIdcardBFileId = item.file_id
            }else if(item.file_type == 'F15'){
              res.data.pUnincorporatedFileId = item.file_id
            }
          })
          }
          
          this.$refs.basicInfo.userType = res.data.merchantType == 3 ? '1' : '2'  
          this.userType = res.data.merchantType == 3 ? '1' : '2'  
          this.minProfitRate = res.data.rate
          res.data.bankId =  res.data.bankId + ''
          if(!this.mchId){
            res.data.mchId = res.data.aliMchId
          }
          if(this.userType == 2){
            this.$refs.enterprise.enterpriseChange1(res.data.merchantMcc1,'none')
            this.$refs.enterprise.enterpriseChange2(res.data.merchantMcc2,'none')
            res.data.mccId1 = res.data.merchantMcc1
            res.data.mccId2 = res.data.merchantMcc2
            res.data.mccId = res.data.merchantMcc3 ? res.data.merchantMcc3 : res.data.mccId
            res.data.areaArr = [ res.data.provinceCode * 1 , res.data.cityCode * 1,res.data.areaId]
            res.data.licenseValidityType =  res.data.businessLicenceExpire == '29991231' ? '1' : '0'
            res.data.legalCertValidityType =  res.data.legaCertExpire == '29991231' ? '1' : '0'
            res.data.certValidityType =  res.data.principalCertExpire == '29991231' ? '1' : '0'
            this.$refs.enterprise.operateForm = res.data
          }else{
            res.data.mccId1 = res.data.merchantMcc1
            res.data.mccId2 = res.data.merchantMcc2
            res.data.mccId = res.data.merchantMcc3 ? res.data.merchantMcc3 : res.data.mccId
            res.data.legalCertValidityType =  res.data.principalCertExpire == '29991231' ? '1' : '0'
            this.$refs.personal.enterpriseChange1(res.data.merchantMcc1,'none')
            this.$refs.personal.enterpriseChange2(res.data.merchantMcc2,'none')
            this.$refs.personal.personalForm = res.data
          }
          if(this.mchId){
            res.data.rate =  res.data.qryWxConfList ? JSON.parse(res.data.qryWxConfList)[0].fee_rate  * 100 : 24
            JSON.parse(res.data.fileInfoList).forEach(item=>{
            if(item.file_type == 'F13'){
              res.data.pBankcardA = item.file_url
            }else if(item.file_type == 'F02' || item.file_type == 'F40'){
              res.data.pIdcardB = item.file_url
            }else if(item.file_type == 'F07'){
              res.data.pLicense = item.file_url
            }else if(item.file_type == 'F22'){
              res.data.pOutside = item.file_url
            }else if(item.file_type == 'F24'){
              res.data.pInside = item.file_url
            }else if(item.file_type == 'F105'){
              res.data.pCashier = item.file_url
            }else if(item.file_type == 'F03' || item.file_type == 'F41'){
              res.data.pIdcardA = item.file_url
            }else if(item.file_type == 'F08'){
              res.data.pOpenAccount = item.file_url
            }else if(item.file_type == 'F56'){
              res.data.pBankIdcardA = item.file_url
            }else if(item.file_type == 'F55'){
              res.data.pBankIdcardB = item.file_url
            }else if(item.file_type == 'F15'){
              res.data.pUnincorporated = item.file_url
            }
          })
          }
          this.merchantInfo = res.data
          this.loading = false
          this.$forceUpdate()
         }else{
          this.$message.error(res.message)
          this.loading = false
         }
        },
        minProfitRateChange(v){
          this.minProfitRate = v
        },
    async initGetAreaAll(){
        const res = await getAreaAll()
        this.regionList = res.data
    },
    async initGetBankInfolist(){
        // const res = await parentlist({inst_no:'60000001',level:1,sign:md5('inst_no=60000001&level=1&key=72fde5d00fb44e5784d80b45fc620167')})
        // this.dLimitList = res.data
    },
    async initGetTopMccByType(parentId,level){
            const res = await getTopMccByType({channelType:'16',level:1})
            this.mccList1 = res.data
    },
    async submit(){
        // console.log(this.$refs.basicInfo.ruleForm.bdId)
        let data = {}
        if(this.mchId){
           const formValid = await this.$refs.basicInfo.$refs.ruleForm ? this.$refs.basicInfo.$refs.ruleForm.validate() : true
           if(formValid){
            if(this.userinfo.rolesId == 4){
              this.merchantInfo.bdId = this.userinfo.fid
            }else{
              this.merchantInfo.bdId =  this.$refs.basicInfo.ruleForm.bdId
            }
            this.merchantInfo.channelId =  this.channelId
              this.btnLoading = true
              this.merchantInfo.instNo = this.$route.query.instNo
              const res = await bindSaveMerchantByChannel(this.merchantInfo)
              if(res.code == 200){
                this.btnLoading = false
                this.$message.success('提交成功！')
                this.$router.push('/entryManagement');
             }else{
                this.btnLoading = false
                this.$message.error(res.message)
             }
           }
        }else{
          if(this.userType == '2'){
            const formValid = await Promise.all([this.$refs.enterprise.$refs.operateFormRules.validate() , this.$refs.basicInfo.$refs.ruleForm && this.$refs.basicInfo.$refs.ruleForm.validate(),this.$refs.cashOut.$refs.formRules && this.$refs.cashOut.$refs.formRules.validate()])
             if(formValid){
                if(this.$refs.enterprise.operateForm.accountType == 2){
                  this.$refs.enterprise.operateForm.settlementType = 1
                }else{
                  this.$refs.enterprise.operateForm.settlementType = 2
                }
                if(this.userinfo.rolesId == 4){
                  this.$refs.enterprise.operateForm.bdId = this.userinfo.fid
                }else{
                  this.$refs.enterprise.operateForm.bdId = this.$refs.basicInfo.ruleForm.bdId
                }
                data = this.$refs.enterprise.operateForm
             }
            }else{
                const formValid = await Promise.all([this.$refs.personal.$refs.personalFormRules.validate() , this.$refs.basicInfo.$refs.ruleForm && this.$refs.basicInfo.$refs.ruleForm.validate(),this.$refs.cashOut.$refs.formRules && this.$refs.cashOut.$refs.formRules.validate()])
                if(formValid){
                  this.$refs.personal.personalForm.accountType = 4
                  this.$refs.personal.personalForm.settlementType = 2
                  this.$refs.personal.personalForm.merchantBusinessName = this.$refs.personal.personalForm.merchantName
                  this.$refs.personal.personalForm.servicePhone =this.$refs.personal.personalForm.contactPhone
                  if(this.userinfo.rolesId == 4){
                      this.$refs.personal.personalForm.bdId = this.userinfo.fid
                    }else{
                      this.$refs.personal.personalForm.bdId = this.$refs.basicInfo.ruleForm.bdId
                    }
                  data = this.$refs.personal.personalForm
                }
            }
            if(this.cashConfig.length){
              data.cashConfig = JSON.stringify(this.cashConfig)
            }
            data.channelId = this.channelId
            let res = {}
            this.btnLoading = true
            data.instNo = this.$route.query.instNo
            if(this.merchantInfo.id){
               res =  await singleChannelModify(data)
            }else{
               
               res = await singleChannelSave(data)
            }
            if(res.code == 200){
              this.btnLoading = false
              this.$message.success('提交成功！')
              this.$router.push('/entryManagement');
            }else{
              this.btnLoading = false
              this.$message.error(res.message)
            }
        }
        
    },
    cashOutConfigChange(v){
      this.cashConfig = []
        if(v.T1Form.cash_type){
          this.cashConfig.push(v.T1Form)
        }
         if(v.DMForm.cash_type){
          this.cashConfig.push(v.DMForm)
        }
      // this.cashConfig = v
    },
    userTypeChange(v){
        this.userType = v
    }
   }
}
</script>
<style> 
.submit{
  display:flex;
  justify-content:center;
}
</style>